
import sys
import pdb

__metaclass__ = type

class Debugger(pdb.Pdb):
    def format_stack_entry(self, frame_lineno, lprefix=': '):
        import linecache, repr
        frame, lineno = frame_lineno
        filename = self.canonic(frame.f_code.co_filename)
        s = '%s(%r)' % (filename, lineno)
        if frame.f_code.co_name:
            s = s + frame.f_code.co_name
        else:
            s = s + "<lambda>"
        if '__args__' in frame.f_locals:
            args = frame.f_locals['__args__']
        else:
            args = None
        if args:
            s = s + repr.repr(args)
        else:
            s = s + '()'
        if '__return__' in frame.f_locals:
            rv = frame.f_locals['__return__']
            s = s + '->'
            s = s + repr.repr(rv)

        from functools import partial
        get = partial(linecache.getline, filename)
        n = lineno
        lines = [get(x) for x in (n-2,n-1,n) if get(x)]
        if lines:
            s = s + '\n  ' + '\n  '.join([l.strip() for l in lines])
        return s


def set_trace():
    Debugger().set_trace(sys._getframe().f_back)
